package com.ruoyi.web.controller.ingredients;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.ingredients.PartIngredients;
import com.ruoyi.system.service.ingredientsService.IPartIngredientsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 辅料Controller
 * 
 * @author ljx
 * @date 2020-07-01
 */
@Controller
@RequestMapping("/system/ingredients")
public class PartIngredientsController extends BaseController
{
    private String prefix = "system/ingredients";

    @Autowired
    private IPartIngredientsService partIngredientsService;

    @RequiresPermissions("system:ingredients:view")
    @GetMapping()
    public String ingredients()
    {
        return prefix + "/ingredients";
    }

    /**
     * 查询辅料列表
     */
    @RequiresPermissions("system:ingredients:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PartIngredients partIngredients)
    {
        startPage();
        List<PartIngredients> list = partIngredientsService.selectPartIngredientsList(partIngredients);
        return getDataTable(list);
    }

    /**
     * 导出辅料列表
     */
    @RequiresPermissions("system:ingredients:export")
    @Log(title = "辅料", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PartIngredients partIngredients)
    {
        List<PartIngredients> list = partIngredientsService.selectPartIngredientsList(partIngredients);
        ExcelUtil<PartIngredients> util = new ExcelUtil<PartIngredients>(PartIngredients.class);
        return util.exportExcel(list, "ingredients");
    }

    /**
     * 新增辅料
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存辅料
     */
    @RequiresPermissions("system:ingredients:add")
    @Log(title = "辅料", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PartIngredients partIngredients)
    {
        partIngredients.setCreateBy(ShiroUtils.getLoginName());
        partIngredients.setCreateTime(new Date());
        return partIngredientsService.insertPartIngredients(partIngredients);
    }

    /**
     * 修改辅料
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        PartIngredients partIngredients = partIngredientsService.selectPartIngredientsById(id);
        mmap.put("partIngredients", partIngredients);
        return prefix + "/edit";
    }

    /**
     * 修改保存辅料
     */
    @RequiresPermissions("system:ingredients:edit")
    @Log(title = "辅料", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PartIngredients partIngredients)
    {
        partIngredients.setUpdateBy(ShiroUtils.getLoginName());
        return (partIngredientsService.updatePartIngredients(partIngredients));
    }

    /**
     * 删除辅料
     */
    @RequiresPermissions("system:ingredients:remove")
    @Log(title = "辅料", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(partIngredientsService.deletePartIngredientsByIds(ids));
    }


    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap)
    {
        PartIngredients partIngredients = partIngredientsService.selectPartIngredientsById(id);
        mmap.put("partIngredients", partIngredients);
        return "system/detail/detail";
    }
}
